package com.zxl.yjb.activity.home.news;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zxl.yjb.R;
import com.zxl.yjb.adapter.NewsImageAdapter;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.NewsDetailBean;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NewsDetailActivity extends BaseActivity {


    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_page_title)
    TextView tv_page_title;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_source)
    TextView tv_source;
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.tv_content_list)
    RecyclerView tv_content_list;
    @BindView(R.id.tv_content)
    TextView tv_content;


    private DataManager dataManager;
    private int id;
    private NewsImageAdapter newsImageAdapter;
    private ArrayList<String> imageList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);
        ButterKnife.bind(this);
        id = getIntent().getIntExtra("id", 0);
        initView();
        getData();
    }

    @Override
    protected void initView() {
        tv_page_title.setText("要闻详情");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        linearLayoutManager.setAutoMeasureEnabled(true);
        tv_content_list.setLayoutManager(linearLayoutManager);
        tv_content_list.setHasFixedSize(true);
        tv_content_list.setNestedScrollingEnabled(false);
        newsImageAdapter = new NewsImageAdapter(R.layout.item_new_image,imageList);
        tv_content_list.setAdapter(newsImageAdapter);
    }


    private void getData() {
        dataManager = new DataManager();
        Map<String, String> map = new HashMap<>();
        map.put("id", id + "");
        ObserverOnNextListener<NewsDetailBean> listener = new ObserverOnNextListener<NewsDetailBean>() {
            @Override
            public void onNext(NewsDetailBean newsDetailBean) {
                imageList.addAll(newsDetailBean.getImgs());
                setView(newsDetailBean);
            }
        };
        dataManager.newseal(new ProgressObserver<NewsDetailBean>(listener, this), map);
    }


    private void setView(NewsDetailBean newsDetailBean) {
        tv_title.setText(newsDetailBean.getTitle());
        tv_source.setText("来源："+newsDetailBean.getSource());
        tv_time.setText(newsDetailBean.getAddtime());
        tv_content.setText(Html.fromHtml(newsDetailBean.getContent()));
        newsImageAdapter.notifyDataSetChanged();
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked(View view) {

        switch (view.getId()) {
        	case R.id.iv_back:
        		finish();
        		break;

        	default:
        		break;
        }

    }
}
